package cn.jobstart.spider.pageProcessor;

import cn.jobstart.common.StringUtil;
import cn.jobstart.common.spider.Job51Util;
import cn.jobstart.common.spider.LiePinUtil;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.spider.TalentJob51Search;
import cn.jobstart.spider.TalentLiePinSearch;
import cn.jobstart.spider.downloader.request.TalentJob51Request;
import cn.jobstart.spider.downloader.request.TalentLiePinRequest;
import cn.jobstart.spider.model.Job51Json;
import cn.jobstart.spider.model.LiePinJson;
import cn.jobstart.spider.model.TalentJob51;
import cn.jobstart.spider.model.TalentLiePin;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job 人才
 * @date 2023-09-18 15:35
 */
@Data
@Slf4j
public class TalentLiePinPageProcessor extends AbstractPageProcessor<TalentLiePinSearch> {



    private LiePinUtil liePinUtil;


    @Override
    public int getChannelFlag() {
        return TalentSubscriptionTaskChannelEnum.LIE_PIN.getType().intValue();
    }


    /**
     * process the page, extract urls to fetch, extract the data and store
     *
     * @param page page
     */
    @Override
    public void process(Page page) {



        String jsonStr = page.getRawText();

        log.info("jsonStr:" + jsonStr);
        JSONObject jsonObject = null;

        LiePinJson liePinJson = null;

        try {


            liePinJson = JSONObject.parseObject(jsonStr, LiePinJson.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (StringUtil.isNullOrEmpty(liePinJson)) {

            page.getResultItems().put("talentList", null);
            page.getResultItems().put("talentSubscriptionTaskId", getTalentSubscriptionTaskId());

            return;
        }


        int flag = liePinJson.getFlag();
        //说明过期token 重新生成一个token
        if (flag==0) {
            liePinUtil.removeToken();

            liePinUtil.initAuthorization();
            page.setSkip(true);
            addTargetRequest(page, getSearch());
            return;
        }
        int currentPage = 1;
        int totalPage = 0;

        if (StringUtil.isNotObjEmpty(liePinJson.getData())) {

            JSONObject dataJson = JSONObject.parseObject(liePinJson.getData().toString());

            JSONObject resultFormJson=dataJson.getJSONObject("cvSearchResultForm");


            currentPage = resultFormJson.getIntValue("curPage");

            totalPage = resultFormJson.getIntValue("totalCount") /20;

           JSONArray dataArray= resultFormJson.getJSONArray("cvSearchListFormList");

           if(StringUtil.isNullOrEmpty(dataArray)){
               getSearch().setCurPage(currentPage + 1);
               addTargetRequest(page, getSearch());
              return;
           }


            List<TalentLiePin> talentList = JSONArray.parseArray(dataArray.toJSONString(), TalentLiePin.class);

            page.getResultItems().put("talentList", talentList);
            page.getResultItems().put("talentSubscriptionTaskId", getTalentSubscriptionTaskId());

        }

        //说明没有超过要爬数据，
        if(currentPage < totalPage && totalPage>0 &&  currentPage * 20 < getPageCount()){

            currentPage=currentPage + 1;
            getSearch().setCurPage(currentPage);

            addTargetRequest(page, getSearch());
        }




    }


    private void addTargetRequest(Page page, TalentLiePinSearch liePinSearch) {

        try {
            TalentLiePinRequest request = new TalentLiePinRequest();
            request.setToken(liePinUtil.getToken());
            request.build(liePinSearch);

            page.addTargetRequest(request);
        } catch (Exception e) {

        }
    }


    /**
     * get the site settings
     *
     * @return site
     * @see Site
     */
    @Override
    public Site getSite() {


        return Site.me().setCycleRetryTimes(1000).setRetrySleepTime(1000).setRetryTimes(3).setSleepTime(1000).setTimeOut(100000).setDomain("lpt.liepin.com");


    }
}
